{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c1e0786",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Some weights of RobertaModel were not initialized from the model checkpoint at finiteautomata/bertweet-base-sentiment-analysis and are newly initialized: ['pooler.dense.bias', 'pooler.dense.weight']\n",
      "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n",
      "emoji is not installed, thus not converting emoticons or emojis into text. Install emoji: pip3 install emoji==0.6.0\n",
      "emoji is not installed, thus not converting emoticons or emojis into text. Install emoji: pip3 install emoji==0.6.0\n",
      "Device set to use cpu\n",
      "Keyword arguments {'cache_dir': 'E:\\\\nlp\\\\model_cache\\\\models--finiteautomata--bertweet-base-sentiment-analysis\\\\snapshots\\\\924fc4c80bccb8003d21fe84dd92c7887717f245'} not recognized.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'label': 'POS', 'score': 0.983642578125}]\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline,AutoTokenizer,AutoModel #导入依赖库\n",
    "\n",
    "#下载模型到本地\n",
    "model_name = \"finiteautomata/bertweet-base-sentiment-analysis\"\n",
    "cache_dir = \"./model_cache\"\n",
    "model = AutoModel.from_pretrained(model_name,cache_dir= cache_dir)\n",
    "\n",
    "#分词器\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name,cache_dir= cache_dir)\n",
    "\n",
    "# print(model)\n",
    "#离线使用model 进行情感分析\n",
    "model_dir = r\"E:\\nlp\\model_cache\\models--finiteautomata--bertweet-base-sentiment-analysis\\snapshots\\924fc4c80bccb8003d21fe84dd92c7887717f245\"\n",
    "classifier = pipeline(\"sentiment-analysis\", model=model_name, cache_dir= model_dir)\n",
    "\n",
    "# 进行情感分析\n",
    "result = classifier(\"i like Hugging Face's transformers library！\")\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fa49e598",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Keyword arguments {'cache_dir': 'E:\\\\nlp\\\\model_cache\\\\models--finiteautomata--bertweet-base-sentiment-analysis\\\\snapshots\\\\924fc4c80bccb8003d21fe84dd92c7887717f245'} not recognized.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'label': 'NEU', 'score': 0.9192514419555664}]\n"
     ]
    }
   ],
   "source": [
    "result = classifier(\"how about you\")\n",
    "print(result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
